
package com.insigma.business.qrcode.soldier._bff.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;

import com.insigma.business.qrcode.common.dto.SystemConfigDTO;
import com.insigma.business.qrcode.common._consumer.SystemConfigConsumer;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.PlacementInfoDTO;
import com.insigma.business.qrcode.soldier.dto.PlacementStatsInfoDTO;
import com.insigma.business.qrcode.soldier.dto.SimpleApiRequest;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.PlacementInfoInsertRequest;
import com.insigma.business.qrcode.soldier.request.PlacementInfoSearchRequest;
import com.insigma.business.qrcode.soldier.request.PlacementInfoUpdateRequest;
import com.insigma.business.qrcode.soldier._bff.PlacementInfoBFFProvider;
import com.insigma.business.qrcode.soldier._consumer.PlacementInfoConsumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Component("placementInfoBFFProvider")
public class PlacementInfoBFFProviderImpl extends BaseProviderImpl implements PlacementInfoBFFProvider {

    @Autowired
    private PlacementInfoConsumer placementInfoConsumer;

    @Autowired
    private SystemConfigConsumer systemConfigConsumer;

    @Override
    public BaseResponse<PlacementStatsInfoDTO> statPlacement(SimpleApiRequest request) {
        log.info("查询统计信息, {}", request);
        request.setEmployeeId(SessionUtils.getGovUserId());
        return placementInfoConsumer.statPlacement(request);
    }

    @Override
    public BaseResponse<String> getCustomerServcieInfo() {
        final SystemConfigDTO systemConfigDTO = systemConfigConsumer.getSystemConfigByName("AZ_CONTACT_NO").getDto();
        BizCheckUtil.checkNotNull(systemConfigDTO, "配置错误");
        return ResponseUtils.buildSuccessResponse(systemConfigDTO.getValue()) ;
    }


    @Override
    public BasePagingResponse<List<PlacementInfoDTO>> searchPlacementInfo(final PlacementInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<PlacementInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页PlacementInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                searchRequest.setEmployeeId(SessionUtils.getGovUserId());
                searchRequest.setYear(String.valueOf(DateUtil.year(new Date())));
                BasePagingResponse<List<PlacementInfoDTO>> listBasePagingResponse = placementInfoConsumer.searchPlacementInfo(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<PlacementInfoDTO> getPlacementInfo(final BaseGetRequest baseGetRequest){

        final BaseResponse<PlacementInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条PlacementInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<PlacementInfoDTO> placementInfoResponse = placementInfoConsumer.getPlacementInfo(baseGetRequest);
                BeanUtil.copyProperties(placementInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_INFO_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertPlacementInfo(final PlacementInfoInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增PlacementInfo的请求参数不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getThroughTrainStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getThroughTrainEndDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getUnifiedStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getUnifiedApplyStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getUnifiedApplyEndDatetime(), "时间不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildCreatorOfGov(insertRequest);
                insertRequest.setEmployeeId(SessionUtils.getGovUserId());
                BaseResponse<String> placementInfoResponse = placementInfoConsumer.insertPlacementInfo(insertRequest);
                BeanUtil.copyProperties(placementInfoResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_INFO_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updatePlacementInfo(final PlacementInfoUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改PlacementInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotNull(updateRequest, "新增PlacementInfo的请求参数不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getThroughTrainStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getThroughTrainEndDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getUnifiedStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getUnifiedApplyStartDatetime(), "时间不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getUnifiedApplyEndDatetime(), "时间不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> placementInfoResponse = placementInfoConsumer.updatePlacementInfo(updateRequest);
                BeanUtil.copyProperties(placementInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_INFO_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateCanApply(PlacementInfoUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "修改PlacementInfo的请求参数不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getCanApply(), "id不能为空");
        SessionUtils.buildModifierOfGov(updateRequest);
        return placementInfoConsumer.updateCanApply(updateRequest);
    }

    @Override
    public BaseResponse<Integer> publishPlacement(PlacementInfoUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotNull(request.getPublished(), "published不能为空");
        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");

        SessionUtils.buildModifierOfGov(request);
        return placementInfoConsumer.publishPlacement(request);
    }

    @Override
    public BaseResponse<Integer> calculateBatchMatchResult(SimpleApiRequest apiRequest) {

        BizCheckUtil.checkNotNull(apiRequest.getId(), "请求不能为空");
        return placementInfoConsumer.calculateBatchMatchResult(apiRequest.getId());
    }


    @Override
    public BaseResponse<Integer> deletePlacementInfo(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除PlacementInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> placementInfoResponse = placementInfoConsumer.deletePlacementInfo(deleteRequest);
                BeanUtil.copyProperties(placementInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.PLACEMENT_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
